package com.tf_cut.backend.util;

import com.tf_cut.backend.agent.vo.AgentCashOrderVo;
import com.tf_cut.backend.payment.service.PaymentQueryService;
import com.tf_cut.backend.platform.vo.GrantDetailVo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class AutoQueryPaymentTask {
    @Resource
    private ConfigSettingUtil configUtil;

    @Resource
    private PaymentQueryService query;

    @Autowired
    private LLPayUtil llPayUtil;

    private Logger logger = LogManager.getLogger(AutoQueryPaymentTask.class);
    private int round = 0;

    //每隔15秒发送一次查询请求
    @Scheduled(fixedDelay = 5000)
    public void queryPaymentStatus(){
        logger.info("查询当前队列个数：" + QueryPaymentQueue.size());

        if(QueryPaymentQueue.size() > 0) {
            Object obj = QueryPaymentQueue.get();

            if (obj instanceof GrantDetailVo) {
                GrantDetailVo detail = (GrantDetailVo) obj;

//            int res = query.queryDetail(detail);
                int res = llPayUtil.query(detail);

                if (res != 0) {
                    logger.info("发放详单：" + detail.getId() + "查询失败，该订单将重新加入队列等待查询.");
                    QueryPaymentQueue.set(detail);
                }
            } else if (obj instanceof AgentCashOrderVo) {
                AgentCashOrderVo cash = (AgentCashOrderVo) obj;

                int res = query.queryCash(cash);

                if (res != 0) {
                    logger.info("提现订单：" + cash.getOrderId() + "查询失败，该订单将重新加入队列等待查询.");
                    QueryPaymentQueue.set(cash);
                }
            }
        }

        logger.info("队列剩余待查询订单个数：" + QueryPaymentQueue.size());
    }
}
